package com.example.weather_plus.ui.weatherforecast.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weather_plus.R;
import com.example.weather_plus.data.model.HourlyWeatherForecast;

import java.util.List;

public class HourlyForecastAdapter extends RecyclerView.Adapter<HourlyForecastAdapter.HourlyForecastViewHolder> {

    private List<HourlyWeatherForecast> forecastList;

    public void setForecastList(List<HourlyWeatherForecast> forecastList) {
        this.forecastList = forecastList;
    }

    private Context context;

    public HourlyForecastAdapter(Context context, List<HourlyWeatherForecast> forecastList) {
        this.context = context;
        this.forecastList = forecastList;
    }

    @NonNull
    @Override
    public HourlyForecastViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.hourly_weather_item, parent, false);
        return new HourlyForecastViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull HourlyForecastViewHolder holder, int position) {
        HourlyWeatherForecast forecast = forecastList.get(position);

        if (position == 0) {
            holder.textViewTime.setText("现在");
        } else {
            holder.textViewTime.setText(Integer.toString(forecast.getFxTime().getHours()) + "时");
        }
        holder.textViewTemp.setText(Integer.toString(forecast.getTemp()));

        String cond = forecast.getText();
        // 确定展示图标
        if (cond.equals("晴")) {
            if(forecast.getFxTime().getHours()>18 || forecast.getFxTime().getHours()<5){
                holder.iv_icon.setImageResource(R.drawable.night);
            }
            else {
                holder.iv_icon.setImageResource(R.drawable.sunny);
            }
        }
        else if (cond.equals("多云")) {
            if(forecast.getFxTime().getHours()>18 || forecast.getFxTime().getHours()<5){
                holder.iv_icon.setImageResource(R.drawable.night_cloudy);
            }
            else {
                holder.iv_icon.setImageResource(R.drawable.cloudy);
            }
        }
        else if (cond.equals("阴")) {
            holder.iv_icon.setImageResource(R.drawable.yin);
        }
        else if (cond.equals("大雨")) {
            holder.iv_icon.setImageResource(R.drawable.heavy_rain);
        }
        else if (cond.equals("阵雨")) {
            holder.iv_icon.setImageResource(R.drawable.thunderstorm);
        }
        else if (cond.contains("雨")) {
            holder.iv_icon.setImageResource(R.drawable.rainy);
        }
        else if (cond.contains("霾")) {
            holder.iv_icon.setImageResource(R.drawable.haze);
        }
        else if (cond.contains("雾")) {
            holder.iv_icon.setImageResource(R.drawable.foggy);
        }
        else if (cond.contains("雪")) {
            holder.iv_icon.setImageResource(R.drawable.snow);
        }
        else if (cond.equals("冷")) {
            holder.iv_icon.setImageResource(R.drawable.cold);
        }
        else if (cond.contains("沙")) {
            holder.iv_icon.setImageResource(R.drawable.dust);
        }
        else if (cond.contains("雹")) {
            holder.iv_icon.setImageResource(R.drawable.hail);
        }


    }

    @Override
    public int getItemCount() {
        return forecastList.size();
    }

    public class HourlyForecastViewHolder extends RecyclerView.ViewHolder {
        TextView textViewTime, textViewTemp;
        ImageView iv_icon;

        public HourlyForecastViewHolder(View itemView) {
            super(itemView);
            textViewTime = itemView.findViewById(R.id.tvTime);
            textViewTemp = itemView.findViewById(R.id.tvTemperature);
            iv_icon = itemView.findViewById(R.id.ivWeatherIcon);
        }
    }

    public void updateData(List<HourlyWeatherForecast> newData) {
        if (newData != null && !newData.equals(forecastList)) { // 确保数据有变化才更新
            forecastList.clear();
            forecastList.addAll(newData);
            notifyDataSetChanged(); // 通知数据集变化
        }
    }

}